package com.briup.jz.service.impl;

import java.util.Date;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.briup.jz.bean.Comment;
import com.briup.jz.bean.CommentExample;
import com.briup.jz.bean.extend.CommentExtend;
import com.briup.jz.dao.CommentMapper;
import com.briup.jz.dao.extend.CommentExtendMapper;
import com.briup.jz.service.ICommentService;
import com.briup.jz.utils.CustomerException;

@Service
public class CommentServiceImpl implements ICommentService {
    
	@Autowired
    private CommentMapper commentMapper;
	@Autowired
	private CommentExtendMapper commentExtendMapper;
	
	@Override
	public void saveOrUpdate(Comment comment) throws CustomerException{
		// TODO Auto-generated method stub
		if(comment.getId() == null) {//id为空，做插入操作
			//初始化
			comment.setCommentTime(new Date().getTime());
			comment.setReplyTime(new Date().getTime());
			comment.setStatus("未审核");
			//插入到数据库
			commentMapper.insert(comment);
		}else {
			commentMapper.updateByPrimaryKey(comment);//更新
		}
		}
		



	@Override
	public void deleteBy(long id) throws CustomerException {
		Comment comment = commentMapper.selectByPrimaryKey(id);
		if(comment == null) {
			throw new CustomerException("要删除的评论不存在");
		}
		commentMapper.deleteByPrimaryKey(id);
		
	}

	@Override
	public List<Comment> query(Long commentId, String comment, String reply, String status, Long userId) {
		// TODO Auto-generated method stub
		//模板查询
		CommentExample example = new CommentExample();
		//条件
		CommentExample.Criteria criteria = example.createCriteria();//模板对象
		if(commentId != null) {
			criteria.andArticleIdEqualTo(commentId); 
		}
		if(comment != null) {
			criteria.andCommentLike("%"+comment+"%");
		}
		if(reply != null) {
			criteria.andReplyLike("%"+reply+"%");
		}
		if(status != null) {
			criteria.andStatusEqualTo(status);
		}
		if(userId != null) {
			criteria.andUserIdEqualTo(userId);
		}
		
		
		
		return commentMapper.selectByExample(example);
	}




	@Override
	public List<CommentExtend> queryCascade(Long articleId, String comment, String reply, String status, Long userId) {
			return commentExtendMapper.select(articleId, comment, reply, status, userId);
		}
	}



